package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.entity.IdentifyImagesInfo;
import com.xjtu.duansheli.mapper.IdentifyImagesInfoMapper;
import com.xjtu.duansheli.service.IdentifyImagesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.duansheli.utils.DocumentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@Service
public class IdentifyImagesInfoServiceImpl extends ServiceImpl<IdentifyImagesInfoMapper, IdentifyImagesInfo> implements IdentifyImagesInfoService {

    @Autowired
    IdentifyImagesInfoMapper identifyImagesInfoMapper;

    @Override
    public List<IdentifyImagesInfo> getAllIdentifyImages(int current, int size) {

        Page<IdentifyImagesInfo> page = new Page<>(current,size);
        identifyImagesInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
//        List<IdentifyImagesInfo> identifyImagesInfos = identifyImagesInfoMapper.selectList(null);
//        if (identifyImagesInfos.size() == 0) {
//            throw new MyException(500, "数据库中无数据！");
//        }
//        return identifyImagesInfos;
    }

    @Override
    public IdentifyImagesInfo getIdentifyImagesByid(Long id) {
        IdentifyImagesInfo identifyImagesInfo = identifyImagesInfoMapper.selectById(id);
        if (identifyImagesInfo == null) {
            throw new MyException(500, "不存在该认证图片！");
        }
        return identifyImagesInfo;
    }

    @Override
    public IdentifyImagesInfo getIdentifyImagesByuid(Long uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",uid);

        List<IdentifyImagesInfo> identifyImagesInfos = identifyImagesInfoMapper.selectByMap(map);
        if(identifyImagesInfos.size() == 0){
            throw new MyException(500,"该用户未进行认证！");
        }
        return identifyImagesInfos.get(0);
    }

    @Override
    public List<IdentifyImagesInfo> getIdentifyImagesByIden() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("is_identify",0);
        List<IdentifyImagesInfo> identifyImagesInfos = identifyImagesInfoMapper.selectByMap(map);
        if(identifyImagesInfos.size() == 0){
            throw new MyException(500,"目前没有待审批的图片！");
        }
        return identifyImagesInfos;
    }

    @Override
    public int addIdentifyImages(MultipartFile file, Long id) {

        if(file == null) throw new MyException(400,"上传失败！");
        IdentifyImagesInfo identifyImagesInfo = new IdentifyImagesInfo();
        try {
            String fileName = file.getOriginalFilename();
            assert fileName != null;
            if(fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("jpeg")){
                Map<String, String> temps = DocumentUtil.saveDocument(fileName);
                String savePath = temps.get("filePath");
                String saveUrl = temps.get("fileUrl");
                identifyImagesInfo.setImageName(fileName)
                        .setImagePath(savePath)
                        .setImageUrl(saveUrl)
                        .setUid(id);
                file.transferTo(new File(savePath));
                return identifyImagesInfoMapper.insert(identifyImagesInfo);
            }else{
                throw new MyException(400, "上传文件格式错误！请上传正确的图片文件！");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new MyException(500, "添加认证图片失败！");
        }
    }

    @Override
    public int updateIdentifyImages(IdentifyImagesInfo identifyImagesInfo) {
        try {
            return identifyImagesInfoMapper.updateById(identifyImagesInfo);
        } catch (Exception e) {
            throw new MyException(500, "更新认证图片失败！");
        }
    }

    @Override
    public int deleteIdentifyImages(Long id) {
        try {
            return identifyImagesInfoMapper.deleteById(id);
        } catch (Exception e) {
            throw new MyException(500, "删除认证图片失败！");
        }
    }

    @Override
    public int getTotal() {
        return identifyImagesInfoMapper.selectCount(null);
    }
}
